package mlc.applet;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;

public class KeyboardManager implements KeyListener {

	private MLCApplet applet;
	private boolean debug = false;
	
	private List<Integer> typed = new CopyOnWriteArrayList<Integer>();
	private Vector<Integer> pressed = new Vector<Integer>();
	
	public KeyboardManager(MLCApplet applet) {
		this.applet = applet;
	}
	
	public void keyPressed(KeyEvent e) {
		
		Integer key = e.getKeyCode();
		//log("keyTyped "+key);
		
		if(!typed.contains(key)){
			//Key has been typed now
			log("Registerd new typed key "+key);
			typed.add(key);
			applet.registerKeyTyped(e);
		}else if(!pressed.contains(key)){
			//Key holding started now
			log("Registered pressing on key "+key);
			pressed.add(key);
		}
	}
	
	public void keyReleased(KeyEvent e) {
		
		Integer key = e.getKeyCode();
		//log("keyReleased "+key);
		
		if(pressed.contains(key)){
			log("Removed pressing status from key "+key);
			pressed.remove(key);
		}
		if(typed.contains(key)){
			log("Released key "+key);
			typed.remove(key);
		}
		applet.registerKeyReleased(e);
	}

	public void keyTyped(KeyEvent e) {
		
	}
	
	private void log(Object value) {
		if(debug)
			System.out.println("KeyManager: "+value);
	}

	public Vector<Integer> getPressed() {
		return pressed;
	}

	public List<Integer> getTyped() {
		return typed;
	}
}
